package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.service.IChannelService;
import com.dkd.manage.service.INodeService;
import com.dkd.manage.service.IVmTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

import static com.dkd.common.constant.DkdContants.VM_STATUS_NODEPLOY;

/**
 * 设备管理Service业务层处理
 * 
 * @author davidwin
 * @date 2024-09-27
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    private IVmTypeService vmTypeService;
    @Autowired
    private INodeService nodeService;
    @Autowired
    private IChannelService channelService;

    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        //1.新增设备
        //1-1自动生成机器编码
        vendingMachine.setInnerCode(UUIDUtils.getUUID());
        //1-2 查询售货机类型，补充设备容量
        VmType vmType = vmTypeService.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        //1-3 查询点位信息-补充详细地址，区域和合作伙伴信息
        Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
        BeanUtils.copyProperties(node, vendingMachine, "id");
        vendingMachine.setAddr(node.getAddress());
        //1-4补充售货机状态，默认为未投放
        vendingMachine.setVmStatus(VM_STATUS_NODEPLOY);
        //1-5补充创建时间,更新时间
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        //1-6保存
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);


        //2-货道信息
        //2-1声明货到集合
        List<Channel> channelList = new ArrayList<>();
        //2-2双for循环，外层循环货道行，内层循环货道列
        for (int i = 1; i <= vmType.getVmRow(); i++) {
            for (int j = 1; j <= vmType.getVmCol(); j++) {
                //2-3创建货道对象
                Channel channel = new Channel();
                //2-4补全货道信息
                channel.setChannelCode(i + "-" + j); //货道编号
                channel.setVmId(vendingMachine.getId());//设备id
                channel.setInnerCode(vendingMachine.getInnerCode()); //售货机编码
                channel.setMaxCapacity(vendingMachine.getChannelMaxCapacity());//货道最大容量
                channel.setCreateTime(DateUtils.getNowDate()); //创建时间
                channel.setUpdateTime(DateUtils.getNowDate()); //更新时间
                channelList.add(channel);
            }
        }
        //3批量保存数据
        channelService.batchInsertChannel(channelList);
        //返回结果
        return result;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        if(vendingMachine.getNodeId()!=null) {
            //根据前端传过来的点位ID 查询点位表里的点位信息，补充详细地址，区域和合作伙伴信息
            Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
            BeanUtils.copyProperties(node, vendingMachine, "id");
            vendingMachine.setAddr(node.getAddress());
        }
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    /**
     * 根据设备编码查询设备信息
     *
     * @param innercode 设备编码
     * @return VendingMachine
     */
    @Override
    public VendingMachine selectVendingMachineByInnercode(String innercode) {

        return vendingMachineMapper.selectVendingMachineByInnercode(innercode);
    }
}
